package com.ddl.kafka.config.direct;

import com.ddl.kafka.config.MessageData;
import com.rabbitmq.client.Channel;

import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * 消息接收者
 */
@Component
@RabbitListener(queues = "countAttendanceQueue")
@Slf4j
public class CountConsumer {

	@RabbitHandler
	/*public void onMessage(MessageData message) {
		log.info("[消费消息][线程编号:{} 消息内容：{}]", Thread.currentThread().getId(), message.getMsg());
	}*/
	public void onMessage(@Payload MessageData message, Channel channel,
						  @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag) throws Exception {
		log.info("[mq消费消息][线程编号:{} 消息内容：{}]", Thread.currentThread().getId(), message.getMsg());
		Thread.sleep(1000);
		//throw new RuntimeException("专门的");
		channel.basicAck(deliveryTag, true);

	}

	@RabbitHandler
	/*public void onMessage(MessageData message) {
		log.info("[消费消息][线程编号:{} 消息内容：{}]", Thread.currentThread().getId(), message.getMsg());
	}*/
	public void onMessage(List<MessageData> messageList, Channel channel,
						  @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag) throws Exception {
		log.info("[mq批量消费消息][线程编号:{} 消息内容：{}]", Thread.currentThread().getId(), messageList.size());

		channel.basicAck(deliveryTag, true);
	}
}
